﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmDSHang : Form
    {
        #region "Variables"
        public string _MaHang;
        public string _Hang;
        public DataTable _dtbHang = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP");
        DataTable _dtbLoaiSP = KetNoi.GetTable("select * from tblLoaiSanPham");

        public int _Max;
        public int _strDG = 0;
        public int _strCL = 0;
        public int _strDT = 0;
        #endregion  
        public frmDSHang()
        {
            InitializeComponent();  
        }

        private void frmDSHang_Load(object sender, EventArgs e)
        {
            cmbLoaiSP.DisplayMember = "TenLoaiSP";
            cmbLoaiSP.ValueMember = "MaLoaiSP";
            cmbLoaiSP.DataSource = _dtbLoaiSP;

            dataGridView1.DataSource = _dtbHang;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
        }

        public string KiemTraDG(string _DG)
        {
            string _Str;
            switch(_DG)
            {
                case "11": _Str = "and DonGiaBan <100000"; break;
                case "12": _Str = "and DonGiaBan <300000 and DonGiaBan >=100000"; break;
                case "13": _Str = "and DonGiaBan <500000 and DonGiaBan >=300000"; break;
                case "14": _Str = "and DonGiaBan >=100000"; break;
                default: _Str = ""; break;
            }
            return _Str;
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex>=0)
            {
                _MaHang = _dtbHang.Rows[e.RowIndex]["TenSP"].ToString();
                _Hang = KetNoi.GetTable("select MaSP from tblSanPham where TenSP='"+_MaHang+"'").Rows[0].ToString() ;
                this.Close();    
            }
        }

        private void rdBDG100_CheckedChanged(object sender, EventArgs e)
        {
            _strDG = 11;
            //string str = "select MaSP,TenSP,DonGiaBan,ChatLieu,MaKho,KhuyenMai from tblSanPham where DonGiaBan < 100000";
            
            if (rdBDG100.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where DonGiaBan < 100000");
            }
        }

        private void rdBDG300_CheckedChanged(object sender, EventArgs e)
        {
            _strDG = 12;
            if (rdBDG300.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where DonGiaBan < 300000 and DonGiaBan>=100000");
            }
        }

        private void rdBDG500_CheckedChanged(object sender, EventArgs e)
        {
            _strDG = 13;
            if (rdBDG500.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where  DonGiaBan < 500000 and DonGiaBan>=300000");
            }
        }

        private void rdBDGmore_CheckedChanged(object sender, EventArgs e)
        {
            _strDG = 14;
            if (rdBDGmore.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP  where DonGiaBan > 500000");
            }
        }

        private void rdBDGhuy_CheckedChanged(object sender, EventArgs e)
        {
            _strDG = 10;
            if (rdBDGhuy.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP");
            }
        }

        private void rdbKLoai_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbKLoai.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where ChatLieu like '%k%'");
            }
        }

        private void rdbCLNhua_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbCLNhua.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where ChatLieu like '%n%'");
            }
        }

        private void rdbCLGo_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbCLGo.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where ChatLieu like '%g%'");
            }
        }

        private void rdBCLKhac_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBCLKhac.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where ChatLieu not like '%k%' and ChatLieu not like '%g%' and ChatLieu not like '%n%'");
            }
        }

        private void rdBCLHuy_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBCLHuy.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP ");
            }
        }

        private void rdBDT1_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBDT1.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where MaDoTuoi = '1D3'");
            }
        }

        private void rdBDT3_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBDT3.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where MaDoTuoi = '3D5'");
            }
        }

        private void rdBDT5_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBDT5.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where MaDoTuoi = '5D10'");
            }
        }

        private void rdBDT10_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBDT10.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where MaDoTuoi = 'O10'");
            }
        }

        private void rdBDTHuy_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBDTHuy.Checked == true)
            {
                dataGridView1.DataSource = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    +"from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP  ");
            }
        }

        private void cmbLoaiSP_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbLoaiSP.SelectedValue != null)
            {
                _dtbHang = KetNoi.GetTable("select tblSanPham.TenSP,tblSanPham.DonGiaBan,tblSanPham.ChatLieu,tblSanPham.NhaSanXuat,tblChiTietKho.SoLuongTon "
                                                    + "from tblSanPham left join tblChiTietKho on tblSanPham.MaSP =tblChiTietKho.MaSP where MaLoaiSP = '" + cmbLoaiSP.SelectedValue.ToString() + "' ");
                dataGridView1.DataSource = _dtbHang;
                dataGridView1.AllowUserToAddRows = false;
            }
        }
    }
}
